package com.wl.bbs;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.wl.bbs.controller.LoginInterceptor;
import org.apache.ibatis.logging.log4j.Log4jImpl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;

import javax.sql.DataSource;

@Configuration
@ComponentScan("com.wl.bbs")
@EnableWebMvc
@MapperScan("com.wl.bbs.dao")
public class BbsConfig implements WebMvcConfigurer {

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer
                .defaultContentType(MediaType.APPLICATION_JSON)
                .mediaType("json", MediaType.APPLICATION_JSON)
                .mediaType("xml", MediaType.APPLICATION_XML);
    }

    //静态资源映射  该配置会导致静态资源不被拦截
//    @Override
//    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
//        configurer.enable();
//    }

    @Bean
    DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/bbs?useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return dataSource;
    }

    @Bean
    MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        /**
         * 加载 mybatis 配置 ( 可选 )
         * setConfiguration 不能和 setConfigLocation 同时使用, 否则报错
         */
        // ClassPathResource classPathResource = new ClassPathResource("mybatis.xml");
        // sqlSessionFactoryBean.setConfigLocation(classPathResource);
        /**
         * MyBatis配置类 等效于 mybatis.xml
         */
        MybatisConfiguration configuration = new MybatisConfiguration();
        /**
         * 配置日志框架 log4j, 等效于
         * <settings>
         *     <setting name="logImpl" value="LOG4J"/>
         * </settings>
         */
        configuration.setLogImpl(Log4jImpl.class);
        /**
         * MyBatis-Plus 分页插件
         */
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setDialect(new MySqlDialect());
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
        configuration.addInterceptor(mybatisPlusInterceptor);
        /**
         *  setConfiguration 不能和 setConfigLocation 同时使用, 否则报错
         */
        sqlSessionFactoryBean.setConfiguration(configuration);
        return sqlSessionFactoryBean;
    }

    @Bean
    MultipartResolver multipartResolver(){
        return new StandardServletMultipartResolver();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("image/head/**")
                .addResourceLocations(
                        //"file:D:/maven项目/yc3/",
                        "file:/root/bbs/upload",
                        "/image/head/");
        registry.addResourceHandler("/**")
//                .addResourceLocations(
//                        "file:D:/maven项目/yc3/bbs/src/main/webapp/");
                .addResourceLocations(
                        "file:/root/gitee/yc3/bbs/src/main/webapp/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("post.html","/back/**");
    }
}